<?php

class ProductsDAO{
    
    public function ProductsDAO(){
    
    }
    
	/**
     * @param Product $product
     * @return true if the excecution was successful, false if opposite
     */
    public function addProduct($product){
        $bd = new Bd();
        
        $sql = "INSERT INTO products (code, reference, name, description, presentation, picture, stock, minStock, price, access) 
        		VALUES ('".$product->getCode()."', '".$product->getReference()."', '".$product->getName()."', '".$product->getDescription()."', '".$product->getPresentation()."', 
                '".$product->getPicture()."', '".$product->getStock()."', '".$product->getMinStock()."', '".$product->getPrice()."', '".$product->getAccess()."');";
        return $bd->doUpdate($sql);
    }
    
    /**
     * @param int $idProduct > 0
     * @return array if the product exists, false otherwise
     */
    public function getProduct($idProduct){
        if($this->existProduct($idProduct)){
            $bd = new Bd();        
            $sql = "SELECT * FROM products WHERE id = ".$idProduct.";";            
            return $bd->doQueryRow($sql);
        }
        return false;
    }
    
    /**
     * @param int $idProduct > 0
     * @return true if the product exists, false otherwise
     */
    public function existProduct($idProduct){
        $bd = new Bd();
        
        $sql = "SELECT COUNT(*) FROM products WHERE id = ".$idProduct.";";
        
        if ($bd->doQueryOne($sql) > 0)
            return true;
        else return false;
    }
    
    /**
     * @param int $idProduct > 0
     * @return true if the product could be deactivated, false otherwise or if it wasn't active
     */
    public function deactivateProduct($idProduct){
        if($this->isProducttActive($idProduct)){
            $bd = new Bd();        
            $sql = "UPDATE products SET access = 'f' WHERE id = ".$idProduct.";";            
            return $bd->doUpdate($sql);
        }
        return false;
    }
    
    /**
     * @param int $idProduct > 0
     * @return true if the product could be activated, false otherwise or if it wasn't inactive
     */
    public function activateProduct($idProduct){
        if(!$this->isProductActive($idProduct)){
            $bd = new Bd();        
            $sql = "UPDATE products SET access = 't' WHERE id = ".$idProduct.";";            
            return $bd->doUpdate($sql);
        }
        return false;
    }

    /**
     * @param int $idProduct > 0
     * @return true if the product is active, false otherwise or if it doesn't exist
     */
    public function isProductActive($idProduct){
        if($this->existProduct($idProduct)){
            $bd = new Bd();        
            $sql = "SELECT COUNT(*) FROM products WHERE id = ".$idProduct." and access = 't';";            
            if ($bd->doQueryOne($sql) > 0)
                return true;
            else return false;
        }
        return false;
    }
    
    /**
     * @return array with all existing products
     */
    public function getProducts(){
        $bd = new Bd();        
        $sql = "SELECT * FROM products;";            
        return $bd->doQueryAll($sql);
    }
    
    /**
     * @return array with all existing products
     */
    public function searchProduct($id){
        $bd = new Bd();        
        $sql = "SELECT * FROM products WHERE productId = ".$id." ;";
        return $bd->doQueryAll($sql);
    }
    
    /**
     * @param string $code valid code
     * @return true if found false otherwise
     */
    public function existProductCode($code){
        $bd = new Bd();        
        $sql = "SELECT COUNT(*) FROM products WHERE code = '".$code."' ;";
        
        if ($bd->doQueryOne($sql) > 0)
            return true;
        else return false;
    }
    
    
    /**
     * @param string $id valid id
     * @return true if found false otherwise
     */
    public function existProductId($id){
        $bd = new Bd();        
        $sql = "SELECT COUNT(*) FROM products WHERE userId = ".$id." ;";
        if ($bd->doQueryOne($sql) > 0)
            return true;
        else return false;
    }    


    /**
     * @return int >= 0 with the matching number of products
     */
    public function howManyProducts(){
        $bd = new Bd();        
        $sql = "SELECT COUNT(*) FROM products;";
        return $bd->doQueryOne($sql);
    }
        
}

?>